package org.lee.tpl.app.group;

import lombok.extern.slf4j.Slf4j;
import org.lee.common.util.JsonUtil;
import org.lee.tpl.app.HttpClient;
import org.lee.tpl.app.NetMall;
import sun.misc.BASE64Encoder;

import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * DangDangNetMall
 * 当当商城
 *
 * @author dralee
 * @date 2021-11-24 13:41:42
 **/
@Slf4j
public class DangDangNetMall extends NetMall {

    public DangDangNetMall(String uid, String password) {
        super(uid, password);
    }

    @Override
    protected boolean login(String uid, String password) {
        log.info("模拟当当用户登录：{},{}", uid, password);
        return true;
    }

    @Override
    protected Map<String, String> reptile(String skuUrl) {
        String content = HttpClient.doGet(skuUrl);
        Pattern p = Pattern.compile("(?<=title\\>).*(?=</title)");
        Matcher m = p.matcher(content);
        Map<String, String> map = new ConcurrentHashMap<>();
        if (m.find()) {
            map.put("name", m.group());
        }
        map.put("price", "133.59");
        log.info("模拟当当商品爬虫解析：{} | {} 元 {}", map.get("name"), map.get("price"), skuUrl);
        return map;
    }

    @Override
    protected String createBase64(Map<String, String> goodsInfo) {
        BASE64Encoder encoder = new BASE64Encoder();
        log.info(("模拟生成当当商品base64海报"));
        return encoder.encode(JsonUtil.toJson(goodsInfo).getBytes(StandardCharsets.UTF_8));
    }
}
